package com.lc.pianostudent.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lc.pianostudent.BaseApplication;
import com.lc.pianostudent.R;
import com.lc.pianostudent.conn.PostLogin;
import com.lc.pianostudent.conn.PostUpLoadTeacher;
import com.lc.pianostudent.conn.PostUserTeacher;
import com.lc.pianostudent.dialog.ToastDialog;
import com.lc.pianostudent.util.Validator;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;
import com.zcx.helper.view.AppUsername;

/**
 * Created by Administrator on 2018/6/25.
 */

public class ChangeTeacherActivity extends BaseActivity implements View.OnClickListener{

    @BoundView(value = R.id.change_teacher_button,isClick = true) private LinearLayout change_teacher_button;
    @BoundView(R.id.old_teacher)private TextView old_teacher;
    @BoundView(R.id.new_teacher) private AppUsername new_teacher;
    //得到旧老师账号
    private PostUserTeacher postUserTeacher = new PostUserTeacher(new AsyCallBack<PostUserTeacher.PostUserTeacherInfo>() {
        @Override
        public void onSuccess(String toast, int type, Object o, PostUserTeacher.PostUserTeacherInfo postUserTeacherInfo) throws Exception {
            old_teacher.setText(postUserTeacherInfo.teacher);
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }
    });

    private PostUpLoadTeacher postUpLoadTeacher = new PostUpLoadTeacher(new AsyCallBack<PostUpLoadTeacher.PostUpLoadTeacherInfo>() {
        @Override
        public void onSuccess(String toast, int type, Object o, PostUpLoadTeacher.PostUpLoadTeacherInfo postUpLoadTeacherInfo) throws Exception {
                        BaseApplication.BasePreferences.setIsLogin(false);
                        finish();
                        BaseApplication.INSTANCE.finishActivity(SettingActivity.class);
                        if (NavigationActivity.onClickProduct != null) {
                            NavigationActivity.onClickProduct.setOnClick();
                        }

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_teacher);

        setBackTrue();
        initView();
    }

    private void initView(){
        setTitleName("更换教师");

        postUserTeacher.user_id = BaseApplication.BasePreferences.readUID();
        postUserTeacher.execute();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.change_teacher_button:
                new ToastDialog(this, R.layout.dialog_change_teacher){


                    @Override
                    public void cancel() {
                        dismiss();
                    }

                    @Override
                    public void confirm() {

                        if (TextUtils.isEmpty(new_teacher.getText().toString().trim())) {
                            UtilToast.show("请输入新老师账号");
                        } else if (!Validator.isMobile(new_teacher.getText().toString().trim())) {
                            UtilToast.show("新老师账号格式错误");
                        }  else {
                            postUpLoadTeacher.user_id = BaseApplication.BasePreferences.readUID();
                            postUpLoadTeacher.teacher = new_teacher.getText().toString().trim();

                            postUpLoadTeacher.execute();

                        }
                    }
                }.show();
        }
    }
}
